package com.delayqueue.core.queue;

import com.delayqueue.common.enums.TaskStatus;
import com.delayqueue.common.task.Task;
import com.delayqueue.core.common.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.DelayQueue;

@Slf4j
@Component
public class QueueData {

    public static DelayQueue<Task> queue = new DelayQueue<>();

    public static void addTask(Task task) {
        queue.add(task);
        log.info("已入队：{}，执行时间：{}，队列大小：{}", task.getExecutorType().getName(), TimeUtil.getLocalDateTime(task.getExecuteTime()), queue.size());
    }

    public static int queueSize() {
        return queue.size();
    }

    public static void delete(Task task) {
        for (Task _task : queue) {
            if (_task.equals(task)) {
                _task.setStatus(TaskStatus.DELETED);
                log.info("已删除：{}，队列大小：{}", task.getExecutorType().getName(), queue.size());
                break;
            }
        }
    }
}
